
package data;
import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

public class ProductInfo {

	private int dbId;
	private String name;
	private String indexname;
	private String units;
	private double price;

	public ProductInfo(int dbId, String name, String index, String unit, double price) {
		this.dbId = dbId;
		this.name = name;
		this.indexname = index;
		this.units = unit;
		this.price = price;
	}
	
	public int getDbId() {
		return dbId;
	}

	public String getName() {
		return name;
	}

	public String getIndex() {
		return indexname;
	}

	public String getUnit() {
		return units;
	}

	public double getPrice() {
		return price;
	}
	
	public final static JsonDeserializer<ProductInfo> deserializer = new JsonDeserializer<ProductInfo>() {

		@Override
		public ProductInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
				throws JsonParseException {
			
			int dbId = json.getAsJsonObject().get("id").getAsInt();
			String name = json.getAsJsonObject().get("name").getAsString();
			String indexname = json.getAsJsonObject().get("indexname").getAsString();
			String units = json.getAsJsonObject().get("units").getAsString();
			double price = json.getAsJsonObject().get("price").getAsDouble();
			
			return new ProductInfo(dbId, name, indexname, units, price);
		}
	};
}
